<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        .container {
            width: 400px;
            margin: 100px auto;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .login-type {
            margin-bottom: 15px;
        }
        .login-type label {
            margin-right: 10px;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .error-message {
            color: red;
            margin-top: 10px;
            display: none;
        }
        .code-group {
            display: none;
        }
        .code-group .code-input {
            width: 60%;
            display: inline-block;
        }
        .code-group button {
            width: 35%;
            margin-left: 5%;
            background-color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2 style="text-align: center;">用户登录</h2>
        <div class="login-type">
            <label><input type="radio" name="loginType" value="1" checked> 用户名密码</label>
            <label><input type="radio" name="loginType" value="2"> 手机号密码</label>
            <label><input type="radio" name="loginType" value="3"> 邮箱密码</label>
            <label><input type="radio" name="loginType" value="4"> 邮箱验证码</label>
        </div>
        <div class="form-group">
            <label id="usernameLabel">用户名</label>
            <input type="text" id="username" placeholder="请输入用户名">
        </div>
        <div class="form-group password-group">
            <label>密码</label>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div class="form-group code-group">
            <label>验证码</label>
            <input type="text" id="code" placeholder="请输入验证码" class="code-input">
            <button onclick="sendCode()">发送验证码</button>
        </div>
        <button onclick="login()">登录</button>
        <div class="error-message" id="errorMessage"></div>
        <div style="text-align: center; margin-top: 15px;">
            还没有账号？<a href="register.html">立即注册</a>
        </div>
    </div>

    <script>
        window.onload = function() {
            // 检查是否已登录
            const user = JSON.parse(localStorage.getItem('user'));
            if (user) {
                window.location.href = '/index.html';
            }
        }

        // 监听登录方式切换
        document.querySelectorAll('input[name="loginType"]').forEach(radio => {
            radio.addEventListener('change', function() {
                const loginType = this.value;
                const usernameLabel = document.getElementById('usernameLabel');
                const passwordGroup = document.querySelector('.password-group');
                const codeGroup = document.querySelector('.code-group');
                const usernamePlaceholder = document.getElementById('username');
                
                // 重置错误信息
                hideError();
                
                switch(loginType) {
                    case '1':
                        usernameLabel.textContent = '用户名';
                        usernamePlaceholder.placeholder = '请输入用户名';
                        passwordGroup.style.display = 'block';
                        codeGroup.style.display = 'none';
                        break;
                    case '2':
                        usernameLabel.textContent = '手机号';
                        usernamePlaceholder.placeholder = '请输入手机号';
                        passwordGroup.style.display = 'block';
                        codeGroup.style.display = 'none';
                        break;
                    case '3':
                        usernameLabel.textContent = '邮箱';
                        usernamePlaceholder.placeholder = '请输入邮箱';
                        passwordGroup.style.display = 'block';
                        codeGroup.style.display = 'none';
                        break;
                    case '4':
                        usernameLabel.textContent = '邮箱';
                        usernamePlaceholder.placeholder = '请输入邮箱';
                        passwordGroup.style.display = 'none';
                        codeGroup.style.display = 'block';
                        break;
                }
            });
        });

        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        function hideError() {
            document.getElementById('errorMessage').style.display = 'none';
        }

        function sendCode() {
            const email = document.getElementById('username').value;
            if (!email) {
                showError('请输入邮箱地址');
                return;
            }
            
            // 验证邮箱格式
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                showError('请输入正确的邮箱地址');
                return;
            }
            
            // 发送验证码请求
            fetch('/auth/sendCode', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ email: email })
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 200) {
                    alert('验证码已发送到您的邮箱');
                } else {
                    showError(result.msg || '验证码发送失败');
                }
            })
            .catch(error => {
                showError('服务器错误，请稍后重试');
                console.error('Error:', error);
            });
        }

        function login() {
            hideError();
            
            const loginType = document.querySelector('input[name="loginType"]:checked').value;
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const code = document.getElementById('code').value;
            
            if (!username) {
                showError('请输入用户名/手机号/邮箱');
                return;
            }
            
            if (loginType !== '4' && !password) {
                showError('请输入密码');
                return;
            }
            
            if (loginType === '4' && !code) {
                showError('请输入验证码');
                return;
            }

            // 构建请求数据
            const data = {
                username: username,
                password: password,
                code: code,
                loginType: parseInt(loginType)
            };

            // 发送登录请求
            fetch('/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => response.json())
            .then(result => {
                if (result.code === 200) {
                    console.log('Login success:', result.data); // 添加调试日志
                    // 保存用户信息和token到localStorage
                    localStorage.setItem('user', JSON.stringify(result.data.user));
                    localStorage.setItem('token', result.data.token);
                    // 跳转到首页
                    window.location.href = '/index.html';
                } else {
                    showError(result.msg || '登录失败');
                }
            })
            .catch(error => {
                showError('服务器错误，请稍后重试');
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html> 